#include"array.h"

Array * newArray() {
	Array * out = malloc(sizeof(Array));
	out->count = 0;
	out->size = 10;

	// Initializing with zeroes (NULL)
	out->array = calloc(out->size, sizeof(void *));

	return out;
}


void deleteArray(Array * array) {
	free(array->array);
	free(array);
}

void * getElementByIndex(Array * array, int index) {
	return index >= 0 ? array->array[index] : NULL;
}


void add(Array * array, void * element) {
	if (array->count + 1 < array->size) {
		array->array[array->count] = element;
	} else {
		array->size += 10;
		array->array = realloc(array->array, array->size*sizeof(void *));
		array->array[array->count] = element;
	}
	array->count++;
}

int getIndexByFunction(Array * array, int(*func)(void * e, void * arg), void * arg) {
        int i;
        for (i = 0; i < array->count; i++)
                if (func(array->array[i], arg) == 1)
                        return i;
	return -1;
}


